package com.xbongbong.saas.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.SpecificationDao;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.SpecificationEntity;
import com.xbongbong.saas.util.SpecificationUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chaoxiong.lei
 * @date 2019/2/14 16:11
 */
@Service
public class SpecificationModel implements IModel<SpecificationEntity> {

    @Resource
    private SpecificationDao dao;

    @Override
    public Integer insert(SpecificationEntity entity) throws XbbException {
        entity.setAddTime(DateTimeUtil.getInt());
        entity.setUpdateTime(DateTimeUtil.getInt());
        return dao.insert(entity);
    }

    @Override
    public Integer update(SpecificationEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return dao.update(entity);
    }

    public Integer updateBatch(List<SpecificationEntity> updateList, String corpid) throws XbbException{
        if (CollectionUtils.isEmpty(updateList)){
            return 0;
        }
        return dao.updateBatch(updateList,corpid);
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return dao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return dao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    public Integer save(SpecificationEntity entity) throws XbbException {
        if (entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    public Integer deleteByKey(Long key, String corpid) {
        return dao.deleteByKey(key, corpid);
    }

    public Integer deleteByKeys(List<Long> keys, String corpid) {
        return dao.deleteByKeys(keys, corpid);
    }

    public Integer deleteByProductId(Long productId, String corpid) {
        return dao.deleteByProductId(productId, corpid);
    }

    public void insertBatch(List<SpecificationEntity> list) {
        dao.insertBatch(list);
    }

    /**
     * 获取所有的规格
     *
     * @param productId 父产品id
     * @param corpid    公司id
     * @author GuoJun.HU
     * @since 3.14
     * 创建时间 2018/1/15 17:20
     */
    public List<SpecificationEntity> getSpecificationList(String corpid, Long productId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, 0);
        param.put("productId", productId);
        param.put("orderByStr", "sort asc");
        return findEntitys(param);
    }

    /**
     * 获取所有的规格
     *
     * @param productId 父产品id
     * @param corpid    公司id
     * @author GuoJun.HU
     * @since 3.14
     * 创建时间 2018/1/15 17:20
     */
    public Map<Long,List<SpecificationEntity>> getSpecificationMap(String corpid, List<Long> productIdIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, 0);
        param.put("productIdIn", productIdIn);
        param.put("orderByStrGroup", "product_id desc,sort asc");
        List<SpecificationEntity> entitys = findEntitys(param);
        Map<Long, List<SpecificationEntity>> specMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (SpecificationEntity specificationEntity : entitys) {
            List<SpecificationEntity> specificationEntityList = new ArrayList<>();
            if (specMap.containsKey(specificationEntity.getProductId())) {
                specificationEntityList = specMap.get(specificationEntity.getProductId());
            }
            specificationEntityList.add(specificationEntity);
            specMap.put(specificationEntity.getProductId(), specificationEntityList);
        }
        return specMap;
    }

    /**
     * 获取所有的规格 格式化数据
     *
     * @param productId 父产品id
     * @param corpid    公司id
     * @author GuoJun.HU
     * @since 3.14
     * 创建时间 2018/1/15 17:20
     */
    public JSONArray getSpecificationArray(String corpid, Long productId) {
        List<SpecificationEntity> specificationList = getSpecificationList(corpid, productId);
        JSONArray specificationArray = new JSONArray();
        for (SpecificationEntity entity : specificationList) {
            JSONObject specificationObj = new JSONObject();
            specificationObj.put(StringConstant.SAAS_LINK_BUSINESS_NAME, entity.getName());
            JSONArray valueArray;
            try {
                valueArray = JSON.parseArray(entity.getValue());
            } catch (JSONException e) {
                valueArray = new JSONArray();
            }
            specificationObj.put(StringConstant.VALUE, valueArray);
            specificationArray.add(specificationObj);
        }
        return specificationArray;
    }

    /**
     * 拼接规格
     * 多规格才做转换
     *
     * @param specification {"规格1":"32G","规格2":"全网通"}
     * @return 32G/全网通
     * @author 周智
     * 创建时间 2017/11/28 16:02
     */
    public String joinSpecification(String specification) {
        return SpecificationUtil.joinSpecification(specification);
    }
}
